package com.ruoyi.biz.homepage.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.biz.category.service.IBizCategoryService;
import com.ruoyi.biz.homepage.domain.HomePageLetterVo;
import com.ruoyi.biz.homepage.domain.HomePageVo;
import com.ruoyi.biz.homepage.mapper.HomePageMapper;
import com.ruoyi.biz.homepage.service.IHomePageService;
import com.ruoyi.biz.letter.domain.BizCityLetter;
import com.ruoyi.biz.organization.domain.BizCompany;
import com.ruoyi.biz.organization.mapper.BizCompanyMapper;
import com.ruoyi.biz.organization.service.IBizCompanyService;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.BizCategory;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 查询地市认领件统计业务层处理
 *
 * @author Fei.Cong
 * @date 2023-04-21
 */
@Slf4j
@Service
public class HomePageServiceImpl implements IHomePageService {

    @Autowired
    private HomePageMapper homePageMapper;

    @Autowired
    private IBizCategoryService bizCategoryService;

    /**
     * 查询地市认领件统计
     */
    @Override
    public List<HomePageVo> totallist(BizCityLetter bizCityLetter) {

        try {
            bizCityLetter.setBillMonths(DateUtils.getMonthRange(bizCityLetter.getBeginDate(), bizCityLetter.getEndDate()));
            //查询地市与部门列表
            List<HomePageVo> list = homePageMapper.selectDeptlist(bizCityLetter);
            // 查询地市下属部门数量
            List<HomePageVo> deptlist = homePageMapper.selectDeptNum();
            //查询地市与部门认领信息分类统计
            List<HomePageLetterVo> letterlist = homePageMapper.selectLetterlist(bizCityLetter);

            List<TreeSelect> catlist = getCategoryList();
            List<HomePageLetterVo> templist = new ArrayList<HomePageLetterVo>();
            for (HomePageVo vo : list) {
                //交办数量=确认数量+退回数量；
                Long jiaobanNum = vo.getJiaobanNum();
                Long querenNum = 0L;  // 确认数量
                Long youliNum = 0L;  // 有理访
                Long wuliNum = 0L; // 无理访

                for (HomePageLetterVo sub : letterlist) {
                    // 关联同一部门下数据
                    if (vo.getDeptId().equals(sub.getDeptId())) {
                        //  是否有理，1有理，2无理，3部分有理
                        if ("1".equals(sub.getIsReasonable())) {
                            youliNum = youliNum + sub.getOnlyTypeNum();
                        } else {
                            wuliNum = wuliNum + sub.getOnlyTypeNum();
                        }
                    }
                }
                //确认数量=有理访数+无理访数；
                querenNum = youliNum + wuliNum;
                vo.setQuerenNum(querenNum);
                vo.setYouliNum(youliNum);
                vo.setYouliTypeNum(youliNum);
                vo.setWuliNum(wuliNum);
                vo.setWuliTypeNum(wuliNum);
                vo.setJiaobanNum(vo.getTuihuiNum()+querenNum);

                List<Long> otherlist = new ArrayList<Long>();
                otherlist.add(youliNum); //有理分类合计
                for (TreeSelect m : catlist) {
                    // 有二级分类
                    if (m.getChildren().size() > 0) {
                        for (TreeSelect kk : m.getChildren()) {
                            Long onlyTypeNum = getOnlyTypeNum(Long.parseLong(m.getValue()), Long.parseLong(kk.getValue()), letterlist, "1", vo.getDeptId());
                            otherlist.add(onlyTypeNum);
                        }
                    } else {
                        Long onlyTypeNum = getOnlyTypeNum(Long.parseLong(m.getValue()), 0L, letterlist, "1", vo.getDeptId());
                        otherlist.add(onlyTypeNum);
                    }
                }
                otherlist.add(wuliNum); //无理分类合计
                for (TreeSelect m : catlist) {
                    // 有二级分类
                    if (m.getChildren().size() > 0) {
                        for (TreeSelect kk : m.getChildren()) {
                            Long onlyTypeNum = getOnlyTypeNum(Long.parseLong(m.getValue()), Long.parseLong(kk.getValue()), letterlist, "2", vo.getDeptId());
                            otherlist.add(onlyTypeNum);
                        }
                    } else {
                        Long onlyTypeNum = getOnlyTypeNum(Long.parseLong(m.getValue()), 0L, letterlist, "2", vo.getDeptId());
                        otherlist.add(onlyTypeNum);
                    }
                }
                vo.setCategoryList(otherlist);
            }
            for (HomePageVo vo : list) {
                A:
                for (HomePageVo deptrow : deptlist) {
                    if (vo.getCityCode().equals(deptrow.getCityCode())) {
                        vo.setMergeRows(deptrow.getJiaobanNum());
                        deptrow.setCityCode(1L);
                        break A;
                    }
                }
//                Long tuihui_tmp = 0L;
//                for (HomePageVo vo2 : list) {
//                    // 确认是同一地市
//                    if (vo.getCityCode().equals(vo2.getCityCode())) {
//                        tuihui_tmp = tuihui_tmp + vo2.getQuerenNum();
//                    }
//                }
//                //退回数量   交办数量=确认数量+退回数量；
//                vo.setTuihuiNum(vo.getJiaobanNum() - tuihui_tmp);
            }
            buildTotalCategory(list);

            return list;
        } catch (Exception e) {
            log.error("{},{}", e, e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据一级，二级，获取结果
     */
    public Long getOnlyTypeNum(Long pCategoryId, Long sCategoryId, List<HomePageLetterVo> letterlist, String isReasonable, Long deptId) {
        Long onlyTypeNum = 0L;
        for (HomePageLetterVo sub : letterlist) {
            // 关联同一部门下数据
            if (sub.getDeptId().equals(deptId)
                    && isReasonable.equals(sub.getIsReasonable())
                    && (Objects.nonNull(sub.getpCategoryId()) &&sub.getpCategoryId().equals(pCategoryId))
                    && (Objects.nonNull(sub.getsCategoryId()) && sub.getsCategoryId().equals(sCategoryId))) {
                onlyTypeNum = sub.getOnlyTypeNum();
            }
        }
        return onlyTypeNum;
    }

    public List<TreeSelect> getCategoryList() {
        LambdaQueryWrapper<BizCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(BizCategory::getOrderNum);
        List<BizCategory> menus = bizCategoryService.list(lambdaQueryWrapper);
        return bizCategoryService.buildMenuTreeSelect(menus);
    }
}
